<script>
/*  api start  */
import { 
	_getDistance  //获取用户到店铺的距离
 } from "@/lib/api/shopServer/info";
/* api end */
/* vuex */
import { mapState, mapMutations } from "vuex";
export default {
    data(){
        return {
            showAuthorizePop:false, //显示隐藏授权弹层
            distanceInfo:{},   //用户到店铺的距离信息
        };
    },
    computed:{
        
    },
    methods:{ //saveShopDetailInfo
        ...mapMutations("login", ["saveGetLocation"]),
        //获取用户到店铺的距离
        _getDistance(){
            return _getDistance(arguments[0]).then(data=>{
                return Promise.resolve(data);
            }).catch(err=>{
                return Promise.reject({
                    getDistance:true
                });
            });
        },
        //保存距离信息
        _saveDistanceInfo(info){
            this.distanceInfo = null;
            this.distanceInfo = info;
        },
        //获取位置经纬度 + 获取与店铺距离
        async _LocationAndDistance(){
            //未登录 or 无门店地址信息
            if(!this.userInfo.customerId||!this.shopDetailInfo.address){
				return Promise.resolve();
            }
            try {
                //获取用户经纬度信息
                const lngLat = await this.ZZgetLocation();
                //console.log(lngLat);
                //保存已授权获取位置信息标识
                this.saveGetLocation(true);
                this._saveDistanceInfo(Object.assign(this.distanceInfo,{
                    cusLng:lngLat.longitude,
                    cusLat:lngLat.latitude
                }));
                //console.log(this.distanceInfo);
                //获取用户到店铺的距离
                const resDis = await this._getDistance({
                    showLoading: false,
                    latitude: this.distanceInfo.cusLat,
                    longitude: this.distanceInfo.cusLng,
                    shopId: this.baseShopId
                });
                //console.log(resDis);
                //
                //let dis = resDis.data<1?`${resDis.data.toFixed(1)}`:resDis.data;
                //
                this._saveDistanceInfo(Object.assign(this.distanceInfo,{
                    dis:`${resDis.data.toFixed(1)}`
                }));
                //
                this.ZZsetTimeout(()=>{
                    this.showAuthorizePop = false;
                },0);
            } catch (error) {
                //console.log(error);
                if(error.notGetLocation){
                    //
                    this.ZZsetTimeout(()=>{
                        this.showAuthorizePop = true;
                    },0);
                }
                //获取用户到店铺的距离失败
                if(error.getDistance){
                    this.$api.showToast("获取当前位置与门店的距离失败");
                }
                // 传出错误信息
                return Promise.reject(error);
            }
        }
    }
}
</script>